﻿--GemCounter v0.1
-- Displays how many red/blue/yellow gems you have
-- Written by Killakhan
-- http://www.wowinterface.com/downloads/author-259436.html

local GemCounter = {}
local addon = GemCounter
local addonName = "GC"
local redGems, blueGems, yellowGems = 0, 0, 0
local blueTexture = "Interface\\Icons\\inv_misc_cutgemsuperior2"
local redTexture =  "Interface\\Icons\\inv_misc_cutgemsuperior6"
local yellowTexture =  "Interface\\Icons\\inv_misc_cutgemsuperior"

addon.f = CreateFrame("Frame", addonName.."main", CharacterFrame)
addon.f:SetScript("OnShow", function(self)
      -- print("on show")
      addon.GetGems()
end)

addon.f:SetScript("OnEvent", function(self, event, ...) 
      if addon[event] then 
         return addon[event](addon, event, ...) 
      end 
end)

addon.f:RegisterEvent("UNIT_INVENTORY_CHANGED")
addon.f:RegisterEvent("PLAYER_LOGIN")

function addon:PLAYER_LOGIN(event, ...)
	addon.GetGems()
end
function addon:UNIT_INVENTORY_CHANGED(event, unit)
	if not unit == "player" then return end
	self:PLAYER_LOGIN()
end

for i = 1, 3 do
   addon["button"..i] = PaperDollItemsFrame:CreateTexture(addonName.."button"..i, "OVERLAY")
   local frame = addon["button"..i]
   frame:SetHeight(15)
   frame:SetWidth(15)
   frame.text = PaperDollItemsFrame:CreateFontString(addonName.."text"..i, "OVERLAY", "NumberFontNormal")
   frame.text:SetPoint("LEFT", frame, "RIGHT", 5, 0)
   frame.text:SetText("")
   if i == 1 then
      frame:SetPoint("BOTTOMLEFT", CharacterFrame, "BOTTOMLEFT", 55, 40)
   else
      frame:SetPoint("BOTTOM", addon["button"..(i-1)], "TOP")
   end
end
addon.button1:SetTexture(blueTexture)
addon.button1.text:SetTextColor(0, 0.6, 1)
addon.button2:SetTexture(redTexture)
addon.button2.text:SetTextColor(1, 0.4, 0.4)
addon.button3:SetTexture(yellowTexture)
addon.button3.text:SetTextColor(1, 1, 0)

function addon.GetGems()
   redGems, blueGems, yellowGems = 0, 0, 0
   for i = 1, 18 do
      local gem1, gem2, gem3
      gem1, gem2, gem3 = GetInventoryItemGems(i)
      if gem1 then
         addon.GetGemColors(gem1)
      end
      if gem2 then
         addon.GetGemColors(gem2)
      end
      if gem3 then
         addon.GetGemColors(gem3)
      end
   end
   addon.button1.text:SetText(blueGems) 
   addon.button2.text:SetText(redGems)
   addon.button3.text:SetText(yellowGems)
end

function addon.GetGemColors(gem)
   --print("checking .. " .. gem)
   local testGem = (select(7, GetItemInfo(gem)))
   if testGem == "紅色" then
      redGems = redGems + 1
   elseif testGem == "藍色" then
      blueGems = blueGems + 1
   elseif testGem == "黃色" then
      yellowGems = yellowGems + 1
   elseif testGem == "綠色" then
      blueGems = blueGems + 1
      yellowGems = yellowGems + 1
   elseif testGem == "紫色" then
      redGems = redGems + 1
      blueGems = blueGems + 1
   elseif testGem == "橘色" then
      redGems = redGems + 1
      yellowGems = yellowGems + 1
   else
      return
   end
   
end

